package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.CollectedStoreEntity;
import com.zw.yzk.kabu.data.network.task.CollectedStoreTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.CollectedStoreMapper;
import com.zw.yzk.kabu.view.view.CollectedStoreView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/15.
 */

public class CollectedStorePresenter implements Presenter {

    private UseCase getStore;
    private AppCompatActivity activity;
    private CollectedStoreView collectedStoreView;
    private CollectedStoreMapper mapper;

    @Inject
    public CollectedStorePresenter(@Named("collectedStoreUseCase")UseCase getStore,
                                   CollectedStoreMapper mapper,
                                   AppCompatActivity activity) {
        this.getStore = getStore;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(CollectedStoreView view) {
        this.collectedStoreView = view;
    }

    public void getCollectedStore(int start) {
        getStore.execute(new GetCollectedStoreSubscriber(activity), new CollectedStoreTask(start));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getStore.unSubscribe();
    }

    private class GetCollectedStoreSubscriber extends BaseSubscriber<CollectedStoreEntity> {

        public GetCollectedStoreSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            collectedStoreView.getCollectedStoreFailed();
        }

        @Override
        public void onNext(CollectedStoreEntity collectedStoreEntity) {
            super.onNext(collectedStoreEntity);
            collectedStoreView.getCollectedStoreSuccess(mapper.transfer(collectedStoreEntity));
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_collected_store_failed);
        }
    }
}
